package com.hgxd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.Slave;
import com.hgxd.entity.vo.pagevo.SlavePageVo;
import com.hgxd.result.Result;
import com.hgxd.service.IEquipmentService;
import com.hgxd.service.ISlaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 从机列表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "从机列表")
@RestController
@RequestMapping("/api/slave")
@CrossOrigin
public class SlaveController {
    @Resource(name = "SlaveServiceImpl")
    private ISlaveService slaveService;

    @Resource(name = "EquipmentServiceImpl")
    private IEquipmentService equipmentService;



    /**
     * 得到所有柜号
     *
     * @return
     */
    @PostMapping("/getAllCntrNo")
    @ApiOperation(value = "得到所有柜号", notes = "得到所有柜号")
    @OperationLogAnnotation(operModule = "从机变量记录管理-得到所有柜号", operType = "获取", operContent = "得到所有柜号")
    public Result<List<String>> getAllCntrNo() {
        return Result.ok(slaveService.getAllCntrNo());
    }

    /**
     * 根据柜号获得下辖的所有从机及其信息
     *
     * @param cntrNo
     * @return
     */
    @PostMapping("/getSlavesByCntrNo/{cntrNo}")
    @ApiOperation(value = "根据柜号获得下辖的所有从机及其信息", notes = "得到所有柜号")
    @OperationLogAnnotation(operModule = "从机变量记录管理-根据柜号获得下辖的所有从机及其信息", operType = "获取", operContent = "根据柜号获得下辖的所有从机及其信息")
    public Result<List<Slave>> getSlavesByCntrNo(@PathVariable("cntrNo") String cntrNo) {
        return Result.ok(slaveService.getSlavesByCntrNo(cntrNo));
    }


    /**
     * 获取从机的在线个数
     *
     * @return
     */
    @GetMapping("getOnlineSlave")
    @ApiOperation(value = "获取从机的在线个数")
    @OperationLogAnnotation(operModule = "设备表-获取从机的在线个数", operType = "获取", operContent = "获取从机的在线个数")
    public Result<Long> getOnlineSlave() {
        LambdaQueryWrapper<Slave> slaveLambdaQueryWrapper = new LambdaQueryWrapper<>();
        slaveLambdaQueryWrapper.eq(Slave::getStatus,1);
        long count = slaveService.count(slaveLambdaQueryWrapper);
        return Result.ok(count);
    }

    /**
     * 根据组织名字来获取最新的设备信息
     */
    @PostMapping("/getNewDataByOrganizationName/{organizationName}")
    @ApiOperation(value = "根据组织名字来获取最新的设备信息", notes = "根据组织名字来获取最新的设备信息")
    @ApiImplicitParam(name = "organizationName", value = "组织名称")
    @OperationLogAnnotation(operModule = "设备表-根据组织名字来获取最新的设备信息", operType = "获取", operContent = "根据组织名字来获取最新的设备信息")
    public Result<List<Slave>> getNewDataByOrganizationName(@PathVariable String organizationName) {
        List<Slave> equipment = slaveService.getNewDataByOrganizationName(organizationName);
        return Result.ok(equipment);
    }

    /**
     * 根据organizationId查询对应从机
     *
     * @param organizationId
     * @return
     */
    @GetMapping("/getSlavesByOrganizationId/{organizationId}")
    @ApiOperation(value = "根据organizationId查询对应从机", notes = "传入对应的组织id(organizationId) 可使用10101测试")
    @OperationLogAnnotation(operModule = "从机列表管理-查询", operType = "查询", operContent = "根据organizationId查询对应从机")
    public Result<List<Slave>> getSlavesByOrganizationId(@PathVariable("organizationId") Integer organizationId) {

        QueryWrapper<Slave> slaveQueryWrapper = new QueryWrapper<>();
        slaveQueryWrapper.in("organization_id", organizationId);
        List<Slave> list = slaveService.list(slaveQueryWrapper);
        return Result.ok(slaveService.getSlavesByOrganizationId(organizationId));
    }

    /**
     * 根据equipmentId查询对应的从机
     *
     * @param equipmentId
     * @return
     */
    @ApiOperation(value = "根据equipmentId查询对应的从机", notes = "传入对应的设备id(equipmentId)")
    @OperationLogAnnotation(operModule = "从机列表管理-查询", operType = "查询", operContent = "根据equipmentId查询对应的从机")
    @GetMapping("/getSlaveByEquipmentId/{equipmentId}")
    public Result<List<Slave>> getSlaveByEquipmentId(@PathVariable("equipmentId") Integer equipmentId) {
        QueryWrapper<Slave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_id", equipmentId);
        return Result.ok(slaveService.list(queryWrapper));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增从机", notes = "传入从机对象，无需传入id")
    @OperationLogAnnotation(operModule = "从机列表管理-新增", operType = "新增", operContent = "新增从机")
    public Result<Boolean> insert(@RequestBody Slave slave) {
        return Result.ok(slaveService.save(slave));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改从机", notes = "需要传入id")
    @OperationLogAnnotation(operModule = "从机列表管理-修改", operType = "修改", operContent = "修改从机")
    public Result<Boolean> update(@RequestBody Slave slave) {
        return Result.ok(slaveService.updateById(slave));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除从机", notes = "传入id")
    @OperationLogAnnotation(operModule = "从机列表管理-删除", operType = "删除", operContent = "根据id删除从机")
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(slaveService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据id删除从机", notes = "传入id")
    @OperationLogAnnotation(operModule = "从机列表管理-删除", operType = "删除", operContent = "根据id删除从机")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(slaveService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部从机", notes = "无需传参")
    @OperationLogAnnotation(operModule = "从机列表管理-查询", operType = "查询", operContent = "查询全部从机")
    public Result<List<Slave>> findAll() {
        return Result.ok(slaveService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询某个从机", notes = "需传入id")
    @OperationLogAnnotation(operModule = "从机列表管理-查询", operType = "查询", operContent = "根据id查询某个从机")
    public Result<Slave> findOne(@PathVariable Integer id) {
        return Result.ok(slaveService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "必传pageName和pageSize可传name")
    @OperationLogAnnotation(operModule = "从机列表管理-分页模糊查询", operType = "分页模糊查询", operContent = "分页模糊查询")
    public Result<Page<Slave>> findPage(@RequestBody SlavePageVo slavePageVo) {
        LambdaQueryWrapper<Slave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StrUtil.isBlank(slavePageVo.getName())
                ,Slave::getName,slavePageVo.getName());
        // 倒序查询
        return Result.ok(slaveService
                .page(new Page<>(slavePageVo.getPageNum()
                        , slavePageVo.getPageSize()), queryWrapper));
    }
}

